package com.bxj.common.utils;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by baixiujiang on 17/12/8.
 */
public class StringUtils {
    public static String toString(Object value, String defaultValue) {
        if (value == null)
            return defaultValue;
        return toString(value);
    }

    public static String toString(Object o) {
        if (o == null)
            return null;
        StringBuffer sb = new StringBuffer();
        if (o instanceof String) {
            return o.toString();
        } else if (o instanceof Collection) {
            for (Object value : (Collection) o) {
                if (sb.length() > 0)
                    sb.append("_");
                sb.append(toString(value.toString()));
            }
        } else if (o.getClass().isArray()) {
            for (int i = 0; i < Array.getLength(o); i++) {
                if (sb.length() > 0)
                    sb.append("_");
                sb.append(toString(Array.get(o, i)));
            }
        } else {
            return o.toString();
        }
        return sb.toString();
    }

    public static void main(String arg[]) {
        Map<String, Object> a = new HashMap<String, Object>();
        a.put("sss", 1);

        System.out.println(a.toString());
        System.out.println(toString(a));
    }
}
